﻿using NCP.Core.DataUtil;
using NCP.Core.IoC;
using NCP.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NCP.Service
{
    public class LimitDrugAPIService: IService
    {
        private SqlSugarClient _client;
        public LimitDrugAPIService()
        {
            _client = DbHelper.GetSqlSugarClient();
        }

        //获取中选药品与非中选药品数量 drugCode 非中选药品
        public QtyAndRate GetQtyAndRate(string drugCode, string deptCode)
        {
            var result = _client.SqlQueryable<QtyAndRate>(@" select convert(decimal(18,2),sum(choseqty)) choseqty,
convert(decimal(18,2),sum(unchoseqty)) unchoseqty,
( case when sum(unchoseqty)+sum(choseqty)=0 then 100 else convert(decimal(18,2),sum(choseqty)/(sum(unchoseqty)+sum(choseqty))*100) end ) rate
from (
select 
isnull(sum(t.qty),0) choseqty,
0 unchoseqty
from PhaMedicalList t
where t.FeeDate>=(select e.BeginDate from GroupPurchase e where e.Code=(select code from  F_GetGpCodeByunChosedrugCode(@drugCode)))
and t.FeeDate<Convert(varchar(10),getDate(),120)
--(select e.EndDate from GroupPurchase e where e.Code=(select code from  F_GetGpCodeByunChosedrugCode(@drugCode)))
and t.deptcode in (select code from  F_GetDeptCode(@deptCode)) 
and t.drugcode =(select p.ChosedrugCode from GroupChoseDrugCompare p where p.UnChosedrugCode=@drugCode
 and p.GpCode in (select code from  F_GetGpCodeByunChosedrugCode(@drugCode)))
 union all
select 
0 choseqty,
sum(unchoseqty*Ratio) unchoseqty
from 
(
select 
t.DrugCode,
(select r.Ratio from GroupChoseDrugCompare r where r.UnChosedrugCode=@drugCode and  r.GpCode=(select code from F_GetGpCodeByunChosedrugCode(@drugCode))) Ratio,
t.qty unchoseqty
from PhaMedicalList t
where t.FeeDate>=(select e.BeginDate from GroupPurchase e where e.Code=(select code from  F_GetGpCodeByunChosedrugCode(@drugCode)))
and t.FeeDate<Convert(varchar(10),getDate(),120)
--(select e.EndDate from GroupPurchase e where e.Code=(select code from  F_GetGpCodeByunChosedrugCode(@drugCode)))
 and t.deptcode in (select code from  F_GetDeptCode(@deptCode))
 and t.drugcode in (select code from F_GetALLUnChosedrugCode(@drugCode)) ) a
 ) s

").AddParameters(new { deptCode = deptCode, drugCode = drugCode, })
.First();
            return result;

        }

        //判断是否是非中选药品信息
        public int IsUnChoseDrug(string drugCode)
        {
            DateTime currentDate = DateTime.Now;
            //var result = _client.Queryable<GroupChoseDrugCompare>().Where(p => p.GpCode.Contains( SqlFunc.Subqueryable<GroupPurchase>().Where(s => s.BeginDate <= @currentDate && s.EndDate > @currentDate).Select(s => s.Code)) && p.UnChosedrugCode == @drugCode).AddParameters(new { drugCode = drugCode, currentDate = currentDate, }).Count();
            string sql = @"select count(*) cnt from GroupChoseDrugCompare n 
             where n.GpCode in (select e.Code from GroupPurchase e 
            where e.BeginDate<= Convert(varchar(10),getDate(),120) 
            and e.EndDate>Convert(varchar(10),getDate(),120))
             and n.UnChosedrugCode='{0}'";
            sql = string.Format(sql, drugCode);
            var result = _client.Ado.GetInt(sql);

            return result;
        }
        //获取中选药品科室任务量和完成比例 参数drugCode为非中选药品编码
        public GroupMission GetGroupMission(string drugCode,string deptCode)
        {
            var result = _client.SqlQueryable<GroupMission>(@"select n.ID,n.GpCode,n.GpName,n.DrugCode, 
DATEDIFF(Day,s.BeginDate,getDate()+1)*CONVERT(decimal(18,2), n.Qyt/(DATEDIFF(Day,s.BeginDate,s.EndDate)+1)) Qyt,
n.Unit,n.Rate*100 Rate,n.ValidFlag,n.OperCode,n.OperDate,n.DeptName
 From GroupMission n 
left join GroupPurchase s on s.Code=n.GpCode
where n.GpCode =(select code from  F_GetGpCodeByunChosedrugCode(@drugCode))
and n.DrugCode= (select r.ChosedrugCode from GroupChoseDrugCompare r 
where r.UnChosedrugCode=@drugCode and r.GpCode= (select code from  F_GetGpCodeByunChosedrugCode(@drugCode))
and n.deptcode in (select r.MergeDeptCode from DeptCompare r where r.DeptCode=@deptCode
union all select @deptCode MergeDeptCode)
)
             ").AddParameters(new { drugCode = drugCode, deptCode = deptCode }).First();
            return result;
        }
        //获取科中选与非中选药品信息
        public ChoseAndUnchoseDrugInfo GetChoseAndUnchoseDrugInfo(string unchosesedrugCode)
        {
            var result = _client.SqlQueryable<ChoseAndUnchoseDrugInfo>(@" select r.UnChosedrugCode,f.DrugName UnChosedrugName,r.ChosedrugCode,o.DrugName ChosedrugName from GroupChoseDrugCompare r
 left join PhaBaseInfo o on r.ChosedrugCode=o.DrugCode
 left join PhaBaseInfo f on f.DrugCode=r.UnChosedrugCode
  where r.GpCode in (select code from F_GetGpCode()) and r.UnChosedrugCode=@unchosesedrugCode").AddParameters(new { unchosesedrugCode= unchosesedrugCode }).First();
            return result;
        }
       //获取药品基本信息
        public PhaBaseInfo GetPhaBaseInfo(string drugCode)
        {
            return _client.Queryable<PhaBaseInfo>().Where(x => x.DrugCode == drugCode).First();
        }
        //获取科室或患者的限制信息 1科室，2 患者
        public LimitDrug GetLimitDrug(string drugCode, string code,string type)
        {
            var result = _client.Queryable<LimitDrug>().Where(t => t.DrugCode == drugCode && t.Code == code && t.Type == type).First();
            return result;
        }
        //获取限制时间数据
        public LimitTime GetLimitTime(string drugCode)
        {
            var result = _client.Queryable<LimitTime>().Where(t => t.DrugCode == drugCode).First();
            return result;
        }
        //限制时间后判断是否允许开立
        public int IsAllow(string inpatientNo, string drugCode, DateTime limitDate, decimal days)
        {
            var result = _client.SqlQueryable<PhaMedicalList>(@"select * from PhaMedicalList t 
                        where   t.drugCode=@drugCode and t.FeeDate>@limitDate-@days and t.InpatientNo=@inpatientNo
                        ").AddParameters(new { drugCode = drugCode, limitDate= limitDate, days= days, inpatientNo = inpatientNo }).Count();
            return result;
        }
    }
}
